﻿using System;
using System.Text;

/* Write a program that encodes and decodes a string using given encryption key (cipher).
 * The key consists of a sequence of characters. The encoding/decoding is done by
 * performing XOR (exclusive or) operation over the first letter of the string with
 * the first of the key, the second – with the second, etc.
 * When the last key character is reached, the next is the first.
 */

namespace EncodeDecode
{
    class EncodeDecode
    {
        static string DecodeEncode(string text, string cipher)
        {
            StringBuilder result = new StringBuilder();

            for (int charIndex = 0, cipherIndex = 0,
                length = text.Length, cipherLastLength = cipher.Length;
                charIndex < length; charIndex++, cipherIndex++)
            {
                if (cipherIndex == cipherLastLength)
                {
                    cipherIndex = 0;
                }
                result.Append((char)(text[charIndex] ^ cipher[cipherIndex]));
            }

            return result.ToString();
        }

        static void Main()
        {
            string message = "This is my secret awesome message!!! ";
            string cipher = "\\ASV*&$*(^%*vs";

            string encodedMessage = DecodeEncode(message, cipher);
            string decodedMessage = DecodeEncode(encodedMessage, cipher);

            Console.WriteLine("Original: {0}", message);
            Console.WriteLine("Encoded: {0}", encodedMessage);
            Console.WriteLine("Decoded: {0}", decodedMessage);
        }
    }
}
